package com.maxd.modules.coupon.service.impl;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.modules.coupon.entity.CouponCard;
import com.maxd.modules.coupon.mapper.CouponCardMapper;
import com.maxd.modules.coupon.service.CouponCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: Laban:yubo_010@163.com
 * @Date: 2020/11/12 15:41
 * @Description:
 */
@Service
public class CouponCardServiceImpl implements CouponCardService {


    @Autowired
    private CouponCardMapper couponCardMapper;


    /**
     * 查询所有卡券  分页 列表
     */
    @Override
    public Result findAll(CouponCard couponCard, Pageable pageable) {
        Example<CouponCard> example1 = Example.of(couponCard);
        return ResultUtil.success(couponCardMapper.findAll(example1,pageable));
    }

    /**
     * 保存卡券
     * @param couponCard
     */
    @Override
    public void save(CouponCard couponCard) {
        couponCardMapper.save(couponCard);
    }

    /**
     * 兑换卡券功能
     * 根据cardId修改userId
     */
    @Override
    public void updateCouponCardByCardId(java.util.Date exchangeTime,String cardId, String userId) {
        couponCardMapper.updateCouponCardByCardId(exchangeTime,cardId,userId);
    }

    /**
     * 根据卡券ID查询卡券使用状态
     */
    @Override
    public CouponCard getCouponCardByCardId(String cardId) {
        if(cardId==null){
            return null;
        }
        return couponCardMapper.getCouponCardByCardId(cardId);
    }

    /**
     * 查询所有卡券 不分页
     */
    @Override
    public List<CouponCard> findAllNoPage(CouponCard couponCard) {
        Example<CouponCard> example1 = Example.of(couponCard);
        List<CouponCard> couponCards = couponCardMapper.findAll(example1);
        if(CollectionUtils.isEmpty(couponCards)){
            return null;
        }
        return couponCards;
    }


    /**
     * 根据ID删除多个卡券
     * @param ids
     */
    @Override
    public void deleteByIdList(String ids) {
        String []id=ids.split(",");
        for(String i:id){
            couponCardMapper.deleteById(Long.parseLong(i));
        }
    }

    /**
     * 删除单个卡券
     */
    @Override
    public void deleteById(Long id) {
        couponCardMapper.deleteById(id);
    }


    /**
     * 查询某个用户的所有卡券
     */
    @Override
    public List<CouponCard> findAllByUserId(String userId) {
        return couponCardMapper.findAllByUserId(userId);
    }


}
